package com.atguigu.daijia.customer.controller;

import com.atguigu.daijia.common.login.GuiguLogin;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.util.AuthContextHolder;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "客户API接口管理")
@RestController
@RequestMapping("/customer")
@SuppressWarnings({"unchecked", "rawtypes"})
@RequiredArgsConstructor
public class CustomerController {

    private final CustomerService customerService;

    /**
     * 微信小程序授权登录
     * @param code 微信小程序临时登录凭证
     * @return
     */
    @Operation(summary ="微信小程序授权登录")
    @GetMapping("/login/{code}")
    public Result<String> wxLogin(@PathVariable("code") String code) {
        return Result.ok(customerService.login(code));
    }

    // /**
    //  * 获取登录用户的信息
    //  * @param token
    //  * @return
    //  */
    // @Operation(summary = "获取登录用户的信息")
    // @GetMapping("/getCustomerLoginInfo")
    // public Result<CustomerLoginVo> getCustomerLoginInfo(@RequestHeader("token") String token) {
    //     CustomerLoginVo customerLoginVo = customerService.getCustomerLoginInfo(token);
    //     return Result.ok(customerLoginVo);
    // }

    /**
     * 获取登录用户的信息
     * @return
     */
    @Operation(summary = "获取登录用户的信息")
    @GuiguLogin
    @GetMapping("/getCustomerLoginInfo")
    public Result<CustomerLoginVo> getCustomerLoginInfo() {
        Long customerId = AuthContextHolder.getUserId();
        CustomerLoginVo customerLoginVo = customerService.getCustomerLoginInfo(customerId);
        return Result.ok(customerLoginVo);
    }

    /**
     * 绑定用户手机号
     * @param updateWxPhoneForm
     * @return
     */
    @Operation(summary = "绑定用户手机号")
    @GuiguLogin// 自定义注解标记该方法执行前需要执行登录校验
    @PostMapping("/updateWxPhone")
    public Result<Boolean> updateWxPhone(@RequestBody UpdateWxPhoneForm updateWxPhoneForm) {
        updateWxPhoneForm.setCustomerId(AuthContextHolder.getUserId());
        // return Result.ok(customerService.updateWxPhone(updateWxPhoneForm));
        return Result.ok(true);
    }
}

